<?php


namespace App\Api\Controllers;


use App\Models\Banner;
use App\Models\BaseModel;
use App\Models\Cate;
use App\Models\Product;
use App\Models\ProductComment;
use App\Models\ProductSku;
use App\Models\ProductSpec;
use Illuminate\Http\Request;
use Validator;

class ShopController extends Controller
{
    //商城轮播图
    public function banner()
    {
        $where = [
            ['type','=','B'],
            ['isshow','=','1'],
        ];
        $banners = Banner::where($where)->orderBy('sort','desc')->get()->toArray();
        foreach ($banners as $key => &$val) {
            $val['url'] = BaseModel::jsonDecodeImg($val['url']);
        }
        return $this->responseData($banners);
    }

    //商城分类
    public function cate(){
        $where = [
            ['isshow','=','1'],
        ];
        $cates = Cate::where($where)->orderBy('score','desc')->get()->toArray();
        foreach ($cates as $key => &$val) {
            $val['img'] = BaseModel::jsonDecodeImg($val['img']);
        }
        return $this->responseData($cates);
    }

    //产品列表
    public function products(Request $request)
    {
        $where = [
           [ 'on_sale', '=', 1]
        ];
        if (!empty($request->input('category_id'))) {
            $where[] = ['category_id', '=', $request->input('category_id')];
        }
        if (!empty($request->input('search'))) {
            $where[] = ['title', 'like', '%'.$request->input('search').'%'];
        }
        $products = Product::getPageList($where);
        return $this->responseData($products);
    }

    //热门产品
    public function hotProducts()
    {
        $where = [
            ['on_sale', '=', 1],
            ['is_hot', '=', 1]
        ];
        $products = Product::where($where)
            ->OrderBy('sold_count','desc')
            ->limit(3)
            ->get()
            ->toArray();
        foreach ($products as $key=>&$val) {
            $val['image'] = BaseModel::jsonDecodeImg($val['image']);
            $val['img_group'] = BaseModel::jsonDecodeImg($val['img_group']);
        }
        return $this->responseData($products);
    }

    //商品详情
    public function detail(Request $request)
    {
        if (!$request->id) {
            return $this->responseFailed('ID必传');
        }
        $product = Product::getOne($request->id);
        $user = auth()->user();
        $product['is_vip'] = $user['level'] > 0? 1 : 0;
        if (!$product) {
            return $this->responseFailed('产品不存在');
        }
        $product['comment'] = ProductComment::getOneByProductId($request->id);
        return $this->responseData($product);
    }

    //通过规格 查 sku 库存查询
    public function productSku(Request $request)
    {
        if (!empty($request->spec_item)) {
            $item = $request->spec_item;
            if(is_string($item)){
                $item = json_decode($item);
            }
            if (is_array($item)) {
                $item = implode('_', $item);
            }
            $where = [
                ['specs', '=', $item]
            ];
        } else {
            if (empty($request->product_sku_id)) {
               return $this->responseFailed('缺少必传参数');
            }
            $where = [
                ['id', '=', $request->product_sku_id],
            ];
        }
        $sku = ProductSku::getSku($where);
        return $this->responseData($sku);
    }

    public function ProductComment(Request $request)
    {
        if (!$request->product_id) {
            return $this->responseFailed('产品ID必传');
        }
        $where = [
            ['product_id', '=', $request->product_id],
        ];
        $data = ProductComment::getPageList($where);
        return $this->responseData($data);
    }
}